package com.buildings.activity.reinforce;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;
import com.buildings.bean.ReinforceResult;
import com.buildings.util.CustomToast;
import java.lang.reflect.Field;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shiyaolei
 * @packager com.buildings.activity.reinforce
 * @date 2020/8/31
 **/
public class CheckUtils {

  public static boolean checkNullEditText(TextView editText, String text) {
    if (editText == null) {
      return false;
    }

    Context context = editText.getContext();
    if (!(context instanceof Activity)) {
      return false;
    }
    String str = editText.getText().toString();
    if (TextUtils.isEmpty(str)) {
      CustomToast.showToast(context, text);
      return false;
    }
    return true;
  }

  public static void setText(String str, TextView text) {
    if (text != null && !TextUtils.isEmpty(str)) {
      text.setText(str);
    }
  }

  public static String checkNotNull(ReinforceResult.ReinforceData data, String str) {
    if (data == null) {
      return "";
    }
    if (TextUtils.isEmpty(str)) {
      return "";
    }
    return str;
  }

  public static String formatImageList(List<String> list) {
    if (list == null || list.size() == 0) {
      return "";
    }
    if (list.size() == 1) {
      return list.get(0);
    }
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < list.size(); i++) {
      if (i > 0) {
        String a = list.get(i);
        if (!TextUtils.isEmpty(a)) {
          builder.append(",").append(a);
        }
      } else {
        builder.append(list.get(0));
      }
    }
    return builder.toString();
  }

  public static List<String> formatList(String image) {
    if (TextUtils.isEmpty(image)) {
      return new ArrayList<>();
    }
    String ay[] = image.split(",");
    return Arrays.asList(ay);
  }


  public static boolean checkObjectField(Object obj) {
    Field[] fields = obj.getClass().getDeclaredFields();
    for (int i = 0, len = fields.length; i < len; i++) {
      // 对于每个属性，获取属性名
      String varName = fields[i].getName();
      try {
        // 获取原来的访问控制权限
        boolean accessFlag = fields[i].isAccessible();
        // 修改访问控制权限
        fields[i].setAccessible(true);
        // 获取在对象f中属性fields[i]对应的对象中的变量
        Object o;
        try {
          o = fields[i].get(obj);
        } catch (IllegalAccessException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        // 恢复访问控制权限
        fields[i].setAccessible(accessFlag);
      } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
        return false;
      }
    }
    return false;
  }
}
